/* =============================================================
 * data_table.js v1.0.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 03/03/2021
 * ============================================================ */

(function (factory) {
    "use strict";
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(jQuery);
    }
}(function ($) {

    let NoticeB = function (options) {
        this.options = $.extend(true, {}, $.fn.noticeB.defaults, options);

        this.$main = $(this.options.main);
        this.$title = $(this.options.title);
        this.$content = $(this.options.content);
        this.$foot = $(this.options.foot);

        this.source = this.options.source;
        this.count = 50;
        this.stop = false;

        this.listen();
        this.render();
    };

    NoticeB.prototype = {

        constructor: NoticeB,

        render: function () {// 渲染

            if (!this.source && Object.is(Object.keys(this.source).length, 1)) {
                return;
            }

            this.displayText();

            this.$main.append(this.$title);
            this.$main.append(this.$content);
            this.$main.append(this.$foot);

            $(document.body).append(this.$main);

            this.show();
        },

        displayText: function () {// 显示文本
            this.$title.find(".j_title").text(this.source.title);
            this.$content.find("span").text(this.source.content);
            this.$foot.find("time").text(this.source.time);
        },

        show: function () {
            this.$main.fadeIn(0);
            this.$main.animate({bottom: "50px"}, 2000, "easeOutQuart");
            // this.$main.animate({bottom: "50px"}, 2000);
            this.countDown();
        },

        hide: function () {
            this.$main.fadeOut(2000);
            this.destroy();
        },

        listen: function () {
            this.$main.on("click", ".j_close", $.proxy(this.hide, this))
                .on("mouseenter", $.proxy(this.mouseEnter, this))
                .on("mouseleave", $.proxy(this.mouseLeave, this));
        },

        mouseEnter: function () {
            this.stop = true;
        },
        mouseLeave: function () {
            this.stop = false;
        },

        countDown: function () {
            let interval = setInterval(() => {
                if (this.stop) return;

                this.$main.find(".j_count").text(this.count);
                if (Object.is(this.count, 0)) {
                    clearInterval(interval);
                    this.hide();
                }
                this.count--;
            }, 1000);
        },

        destroy: function () {
            this.$main.remove();
        },
    };

    let old = $.fn.noticeB;

    $.fn.noticeB = function (options) {
        return new NoticeB(options);
    };

    $.fn.noticeB.defaults = {
        main: "<div class='notice_b'></div>",
        title: "<div class='title flex_row jc_spa_between'>" +
        "<span>" +
        "<span class='j_title'></span>&nbsp;" +
        "<span class='j_count'></span>" +
        "</span>" +
        "<span><strong class='j_close'>&times;</strong></span>" +
        "</div>",
        content: "<div class='content'>" +
        "<span></span>" +
        "</div>",
        foot: "<div class='foot flex_row jc_flex_end'>" +
        "<time></time>" +
        "</div>",
        source: [],
    };

    $.fn.noticeB.Constructor = NoticeB;

    $.fn.noticeB.noConflict = function () {
        $.fn.noticeB = old;
        return this;
    };
}));
